package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TExamResults;
import com.ruoyi.system.service.ITExamResultsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 考试成绩Controller
 * 
 * @author ruoyi
 * @date 2024-12-13
 */
@Controller
@RequestMapping("/system/results")
public class TExamResultsController extends BaseController
{
    private String prefix = "system/results";

    @Autowired
    private ITExamResultsService tExamResultsService;

    @RequiresPermissions("system:results:view")
    @GetMapping()
    public String results()
    {
        return prefix + "/results";
    }

    /**
     * 查询考试成绩列表
     */
    @RequiresPermissions("system:results:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TExamResults tExamResults)
    {
        startPage();
        List<TExamResults> list = tExamResultsService.selectTExamResultsList(tExamResults);
        return getDataTable(list);
    }

    /**
     * 导出考试成绩列表
     */
    @RequiresPermissions("system:results:export")
    @Log(title = "考试成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TExamResults tExamResults)
    {
        List<TExamResults> list = tExamResultsService.selectTExamResultsList(tExamResults);
        ExcelUtil<TExamResults> util = new ExcelUtil<TExamResults>(TExamResults.class);
        return util.exportExcel(list, "基础数据1");
    }

    /**
     * 新增考试成绩
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存考试成绩
     */
    @RequiresPermissions("system:results:add")
    @Log(title = "考试成绩", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TExamResults tExamResults)
    {
        return toAjax(tExamResultsService.insertTExamResults(tExamResults));
    }

    /**
     * 修改考试成绩
     */
    @RequiresPermissions("system:results:edit")
    @GetMapping("/edit/{courseId}")
    public String edit(@PathVariable("courseId") Long courseId, ModelMap mmap)
    {
        TExamResults tExamResults = tExamResultsService.selectTExamResultsByCourseId(courseId);
        mmap.put("tExamResults", tExamResults);
        return prefix + "/edit";
    }

    /**
     * 修改保存考试成绩
     */
    @RequiresPermissions("system:results:edit")
    @Log(title = "考试成绩", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TExamResults tExamResults)
    {
        return toAjax(tExamResultsService.updateTExamResults(tExamResults));
    }

    /**
     * 删除考试成绩
     */
    @RequiresPermissions("system:results:remove")
    @Log(title = "考试成绩", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tExamResultsService.deleteTExamResultsByCourseIds(ids));
    }

    @Log(title = "考试成绩", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:results:import")
    @PostMapping("/importData")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TExamResults> util = new ExcelUtil<TExamResults>(TExamResults.class);
        List<TExamResults> userList = util.importExcel(file.getInputStream());
        String message = tExamResultsService.importData(userList, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("system:results:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TExamResults> util = new ExcelUtil<TExamResults>(TExamResults.class);
        return util.importTemplateExcel("基础数据1模版");
    }
}
